<?php

/**
 * ECSHOP 商品页
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: goods.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

$goods_id = !empty($_GET['id']) ? intval($_GET['id']) : '';
$act = !empty($_GET['act']) ? $_GET['act'] : '';
//if ($_SESSION['user_id'] > 0)
//{
//	$smarty->assign('user_name', $_SESSION['user_name']);
//}
/*------------------------------------------------------ */
//-- 改变属性、数量时重新计算商品价格
/*------------------------------------------------------ */
$_LANG['kilogram'] = '千克';
$_LANG['gram'] = '克';
$_LANG['home'] = '首页';
$_LANG['goods_attr'] = '';

if (empty($_REQUEST['act']) && $goods_id)
{
	$goods_info = app_goods_info($goods_id);
	if ($goods_info)
	{
		$goods_info['goods_id'] = $goods_id;						
		$goods_info['goods_name'] = encode_output($goods_info['goods_name']);
		$goods_info['shop_price'] = encode_output($goods_info['shop_price']);
		$goods_info['score_detail'] = encode_output($goods_info['score_detail']);
		/* 我想要 */
		if($goods_info['want_count']){
			$uid = !empty($_GET['uid']) ? intval($_GET['uid']) : '';
			if($uid){
				$sql=" select id from ".$GLOBALS['ecs']->table('goods_want')." where user_id='$uid' and goods_id = '$goods_id' ";

				if($GLOBALS['db']->getOne($sql)){
					$goods_info['wanted']=1;
				}else{
					$goods_info['wanted']=0;
				}
			}else{
					$goods_info['wanted']=0;
			}			
		}else{
			$goods_info['wanted'] = 0;				
			$goods_info['want_count'] = 0;	
		}
			
		/* 我有了 */
//		$goods_info['owned'] = 0;						
//		$goods_info['own_count'] = 0;
		$sql=" select count(id) from ".$GLOBALS['ecs']->table('goods_have')." where goods_id = '$goods_id' ";
		$goods_info['own_count']=$GLOBALS['db']->getOne($sql);
		if($goods_info['own_count']){
			$uid = !empty($_GET['uid']) ? intval($_GET['uid']) : '';
			if($uid){
				$sql=" select id from ".$GLOBALS['ecs']->table('goods_have')." where user_id='$uid' and goods_id = '$goods_id' ";

				if($GLOBALS['db']->getOne($sql)){
					$goods_info['owned']=1;
				}else{
					$goods_info['owned']=0;
				}
			}else{
					$goods_info['owned']=0;
			}			
		}else{
			$goods_info['owned'] = 0;				
			$goods_info['own_count'] = 0;	
		}			
		/* 收藏 */
//		$goods_info['marked'] = 0;						
		$sql=" select count(id) from ".$GLOBALS['ecs']->table('goods_mark')." where goods_id = '$goods_id' ";
		$goods_info['mark_count']=$GLOBALS['db']->getOne($sql);
		if($goods_info['mark_count']){
			$uid = !empty($_GET['uid']) ? intval($_GET['uid']) : '';
			if($uid){
				$sql=" select id from ".$GLOBALS['ecs']->table('goods_mark')." where user_id='$uid' and goods_id = '$goods_id' ";

				if($GLOBALS['db']->getOne($sql)){
					$goods_info['marked']=1;
				}else{
					$goods_info['marked']=0;
				}
			}else{
					$goods_info['marked']=0;
			}			
		}else{
			$goods_info['marked'] = 0;				
			$goods_info['mark_count'] = 0;	
		}	
		/* 外观速览 */
		if ($goods_info['video_url']){
			$goods_info['video_url'] = OSS_VIDEO.$goods_info['video_url'];							
		}
		/* 头图 */
		if ($goods_info['goods_img']){
			$goods_info['share_img'] = OSS_HOST.get_image_path($goods_info['goods_id'],$goods_info['goods_img'],false,'goods');												
		}
		/* 众测得分 */
		$goods_info['score_test'] = 0;								
		/* 用户评分 */		
		$goods_info['score_user'] = 0;			
		/* 相关评测 */			
		$goods_info['related_eva'] = goods_related_article($goods_id, 1, 3);					
		/* 相关资讯 */			
		$goods_info['related_news'] = goods_related_article($goods_id, 2, 3);		
		/* 相关装备 */
//		if ($goods_info['series_id']>0)
//		{
			$goods_info['related_goods'] = related_goods($goods_info, 4);										
//		}
		/* 属性标签 */
		$goods_info['level']>0 ? $goods_info['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$goods_info['level']) : $goods_info['level'] = "";
		$goods_info['intelligent']>0 ? $goods_info['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$goods_info['intelligent']) : $goods_info['intelligent'] = "";
		$goods_info['property']>0 ? $goods_info['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$goods_info['property']) : $goods_info['property'] = "";								
	}
	
	$result['data'] = $goods_info;
	
	$pic_arr = app_goods_gallery($goods_id);
	
	$pictures = array();	
	foreach ($pic_arr as $key => $picture)
	{
		$picture['img_url'] = OSS_HOST.$picture['img_url'];
		$pictures[$key] = $picture;
	}

	$result['pictures'] = $pictures;

	//图库总数
    $sql = 'SELECT count(*) ' .
    ' FROM ' . $GLOBALS['ecs']->table('goods_gallery') ." AS g ".    
	" LEFT JOIN " .$GLOBALS['ecs']->table('picture_type'). " AS p ".	
    " ON g.img_info = p.picture_id ".                
    " WHERE g.goods_id = '$goods_id' AND p.picture_name != '轮播'";	
	$result['pic_count'] = $GLOBALS['db']->getOne($sql);

	$result['properties'] = app_goods_properties($goods_id);  // 获得商品的规格和属性
// 	echo $goods_info['cat_name'];
	if($goods_info['cat_name']=="男子路跑鞋" || $goods_info['cat_name']=="女子路跑鞋" || $goods_info['cat_name']=="男子越野鞋" || $goods_info['cat_name']=="女子越野鞋"){
		$cat = get_technology_list($goods_info);
		$result['technologys']=$cat;
	}
	
	//var_dump($result['properties']['pro']);
	echo json_encode($result);	
}

else if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'gallery' && $goods_id)
{
	$pic_arr = get_gallery_for_group($goods_id);	
	$pictures = array();	
	foreach ($pic_arr as $key => $picture)
	{		
		$picture['img_url'] = OSS_HOST.$picture['img_url'];
		$picture['thumb_url'] = OSS_HOST.$picture['thumb_url'];		
		$pictures['value'][] = array(
															'value'        => $key,
															'img_url'        => $picture['img_url'],
															'thumb_url'        => $picture['thumb_url']);
//		if (isset($pictures[$picture['picture_id']]))
//		{
//	        $pictures[$picture['picture_id']]['value'][] = array(
//															'img_url'        => $picture['img_url'],
//															'thumb_url'        => $picture['thumb_url']);						
//		}
//		else
//		{			
//	        $pictures[$picture['picture_id']]['c_id']  = $picture['picture_id'];			
//	        $pictures[$picture['picture_id']]['name']  = $picture['picture_name'];
//	        $pictures[$picture['picture_id']]['value'][] = array(
//															'img_url'        => $picture['img_url'],
//															'thumb_url'        => $picture['thumb_url']);									
//												
//		}
		
		//$pictures[$key] = $picture;
	}

	$result['gallery'] = $pictures;
	
	echo json_encode($result);	
}
/*我想要的商品插入移除*/
else if ($_REQUEST['act'] == 'product_want')
{
	$gid=isset($_GET['gid']) ? $_GET['gid'] : '';
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$state=isset($_GET['state'])?$_GET['state']:'';
	$time = gmtime();
	if($state){
		$sql=" delete from ".$GLOBALS['ecs']->table('goods_want')." where goods_id = '$gid' and user_id = '$uid' ";
//		$sql=" delete "
	}else{
		$sql=" select id from".$GLOBALS['ecs']->table('goods_want')." where goods_id = '$gid' and user_id = '$uid' ";
		if($GLOBALS['db']->getOne($sql)){
			$uu['error']=0;
			echo json_encode($uu);
			return;
		}else{

			$sql=" insert ".$GLOBALS['ecs']->table('goods_want')." (goods_id,user_id,create_time) values ('$gid','$uid','$time')";
		}
		
	}
	if($GLOBALS['db']->query($sql))
	{
		$uu['error']=0;
	}else
	{
		$uu['error']=1;
	}
	echo json_encode($uu);
}
/*收藏产品*/
else if ($_REQUEST['act'] == 'product_mark')
{
	$gid=isset($_GET['gid']) ? $_GET['gid'] : '';
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$state=isset($_GET['state'])?$_GET['state']:'';
	$time = gmtime();
	if($state){
		$sql=" delete from ".$GLOBALS['ecs']->table('goods_mark')." where goods_id = '$gid' and user_id = '$uid' ";
//		$sql=" delete "
	}else{
		$sql=" select id from".$GLOBALS['ecs']->table('goods_mark')." where goods_id = '$gid' and user_id = '$uid' ";
		if($GLOBALS['db']->getOne($sql)){
			$uu['error']=0;
			echo json_encode($uu);
			return;
		}else{

			$sql=" insert ".$GLOBALS['ecs']->table('goods_mark')." (goods_id,user_id,create_time) values ('$gid','$uid','$time')";
		}
		
	}
	if($GLOBALS['db']->query($sql))
	{
		$uu['error']=0;
	}else
	{
		$uu['error']=1;
	}
	echo json_encode($uu);
}
/*我是否对我已有的商品进行评价*/
else if ($_REQUEST['act'] == 'product_own')
{
	$gid=isset($_GET['gid']) ? $_GET['gid'] : '';
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$state=isset($_GET['state'])?$_GET['state']:'';

	$sql=" select count(id) from".$GLOBALS['ecs']->table('goods_have')." where goods_id = '$gid' and user_id = '$uid' ";
	if($GLOBALS['db']->getOne($sql)){
			$uu['error']=0;
	}else{
			$uu['error']=1;
	}
	echo json_encode($uu);
}
/*对已有的产品进行评价*/
else if ($_REQUEST['act'] == 'product_sendhave')
{
	$gid=isset($_GET['gid']) ? $_GET['gid'] : '';
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$buy_time=isset($_GET['buy_time'])?$_GET['buy_time']:'';
	$price=isset($_GET['price'])?$_GET['price']:'';
	$score=isset($_GET['score'])?$_GET['score']:'';
	$comments=isset($_GET['comments'])?$_GET['comments']:'';
	$time = gmtime();
	$sql =" insert into ".$GLOBALS['ecs']->table('goods_have')."(user_id,goods_id,create_time,buy_time,buy_price,score,comment,state) values ('$uid','$gid','$time','$buy_time','$price','$score','$comments',1)";
	if($GLOBALS['db']->query($sql)){
			$uu['error']=0;
	}else{
			$uu['error']=1;
	}
	echo json_encode($uu);
}
/*获取我想要列表*/
else if($_REQUEST['act']=='myWant'){
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$pageNo=isset($_GET['pageNo']) ? $_GET['pageNo'] : 0 ;
	$pageSize=isset($_GET['pageSize']) ? $_GET['pageSize'] : 10 ;
	$pageNo=$pageNo-1;
	$sql=" select gw.id,g.goods_name,g.goods_id,g.goods_img,shop_price,market_unit from ".$GLOBALS['ecs']->table('goods_want')." as gw ".
	" left join ".$GLOBALS['ecs']->table('goods')." as g on g.goods_id =gw.goods_id ".
	" where  gw.user_id = '$uid' ".
	" ORDER BY gw.create_time desc ".
	"limit ".$pageNo*$pageSize." , ".$pageSize;
	$uu=$GLOBALS['db']->getAll($sql);
	foreach ($uu as $key => $picture)
	{
		if($picture['goods_img']){
			$uu[$key]['goods_img'] = OSS_HOST.$picture['goods_img'];
		}
		
	}
	echo json_encode($uu); 
}
/*获取我收藏列表*/
else if($_REQUEST['act']=='mymark'){
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$pageNo=isset($_GET['pageNo']) ? $_GET['pageNo'] : 0 ;
	$pageSize=isset($_GET['pageSize']) ? $_GET['pageSize'] : 10 ;
	$selected=isset($_GET['selected']) ? $_GET['selected'] : 26 ;
	$pageNo=$pageNo-1;
	if($selected==1){
		$top_id=26;
	}
	else if($selected==2){
		$top_id=27;
	}
	else if($selected==3){
		$top_id=30;
	}
	else if($selected==4){
		$top_id=73;
	}

	$sql=" select c.top_id,gw.id,g.goods_name,g.goods_id,g.goods_img,shop_price,market_unit from ".$GLOBALS['ecs']->table('goods_mark')." as gw ".
	" left join ".$GLOBALS['ecs']->table('goods')." as g on g.goods_id =gw.goods_id ".
	" left join ".$GLOBALS['ecs']->table('category')." as c on g.cat_id =c.cat_id ".
	" where  gw.user_id = '$uid' and  c.top_id = '$top_id' ".
	" ORDER BY gw.create_time desc ".
	" limit ".$pageNo*$pageSize." , ".$pageSize;
	$uu=$GLOBALS['db']->getAll($sql);
	foreach ($uu as $key => $picture)
	{
		if($picture['goods_img']){
			$uu[$key]['goods_img'] = OSS_HOST.$picture['goods_img'];
		}
		
	}
	echo json_encode($uu); 
}

/*获取我的装备列表鞋柜列表*/
else if($_REQUEST['act']=='havegoods'){
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$pageNo=isset($_GET['pageNo']) ? $_GET['pageNo'] : 0 ;
	$pageSize=isset($_GET['pageSize']) ? $_GET['pageSize'] : 10 ;
	$selected=isset($_GET['selected']) ? $_GET['selected'] : 26 ;
	$pageNo=$pageNo-1;
	if($selected==1){
		$top_id=26;
	}
	else if($selected==2){
		$top_id=27;
	}
	else if($selected==3){
		$top_id=30;
	}
	else if($selected==4){
		$top_id=73;
	}
	$sql=" select c.top_id,gh.id,gh.state,gh.score,g.goods_name,g.goods_id,g.goods_img,shop_price,market_unit from ".$GLOBALS['ecs']->table('goods_have')." as gh ".
	" left join ".$GLOBALS['ecs']->table('goods')." as g on g.goods_id =gh.goods_id ".
	" left join ".$GLOBALS['ecs']->table('category')." as c on g.cat_id =c.cat_id ".
	" where  gh.user_id = '$uid' and  c.top_id = '$top_id' ".
	" ORDER BY gh.create_time desc ".
	"limit ".$pageNo*$pageSize." , ".$pageSize;
	$uu=$GLOBALS['db']->getAll($sql);
	foreach ($uu as $key => $picture)
	{
		if($picture['goods_img']){
			$uu[$key]['goods_img'] = OSS_HOST.$picture['goods_img'];
			
		}
		$uu[$key]['state'] = intval($picture['state']);
		
	}
	echo json_encode($uu); 
}

/*删除选中的我想要*/
else if($_REQUEST['act']=='deletemyWant'){
	$wid=isset($_GET['wid']) ? $_GET['wid'] : '';
	$sql=" delete from ".$GLOBALS['ecs']->table('goods_want')." where id = '$wid' ";
	if($GLOBALS['db']->query($sql))
	{
		$uu['error']=0;
	}else
	{
		$uu['error']=1;
	}
	echo json_encode($uu); 
}
/*选中的我想要*/
else if($_REQUEST['act']=='addequipment'){
	$uid=isset($_GET['uid']) ? $_GET['uid'] : 0;
	$gid=isset($_GET['gid']) ? $_GET['gid'] : 0;
	$time = gmtime();
	$sql="select id from ".$GLOBALS['ecs']->table('goods_have')." where user_id = '$uid' and goods_id = '$gid' ";
	if($GLOBALS['db']->getone($sql)){
		$uu['error']=1;
		$uu['message']="您的装备库里面已经有了这个产品";
	}else{
		$sql="insert into ".$GLOBALS['ecs']->table('goods_have')." (user_id,goods_id,create_time,state) values ('$uid', '$gid', '$time',1)";
		if($GLOBALS['db']->query($sql))
		{
			$uu['error']=0;
			$uu['message']="加入您的装备库成功";
		}else
		{
			$uu['error']=1;
			$uu['message']="加入装备库失败";
		}
	}

	echo json_encode($uu); 
//	echo json_encode($uu); 
}
/*删除选中的我关注的*/
else if($_REQUEST['act']=='deletemymark'){
	$wid=isset($_GET['wid']) ? $_GET['wid'] : '';
	$sql=" delete from ".$GLOBALS['ecs']->table('goods_mark')." where id = '$wid' ";
	if($GLOBALS['db']->query($sql))
	{
		$uu['error']=0;
	}else
	{
		$uu['error']=1;
	}
	echo json_encode($uu); 
}
/*退役装备*/
else if($_REQUEST['act']=='deletehavegoods'){
	$wid=isset($_GET['wid']) ? $_GET['wid'] : '';
	$sql=" update ".$GLOBALS['ecs']->table('goods_have')." set state='0' where id = '$wid' ";
	if($GLOBALS['db']->query($sql))
	{
		$uu['error']=0;
	}else
	{
		$uu['error']=1;
	}
	echo json_encode($uu); 
}
/*获取排行榜最新列表鞋柜列表*/
else if($_REQUEST['act']=='get_new_goods'){
	$pageNo=isset($_GET['pageNo']) ? $_GET['pageNo'] : 0 ;
	$pageSize=isset($_GET['pageSize']) ? $_GET['pageSize'] : 10 ;
	$selected=isset($_GET['selected']) ? $_GET['selected'] : 26 ;
	$pageNo=$pageNo-1;			
		 $sql = "SELECT  g.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
//          "left join ".$GLOBALS['ecs']->table('goods_mark')." as gw on gw.goods_id=g.goods_id ".
            " where  g.is_new = 1 ".
            "limit ".$pageNo*$pageSize." , ".$pageSize;
    $row = $GLOBALS['db']->getAll($sql);
	
	foreach ($row as $key => $good)
	{
		$row[$key]['level'] = '';		
		if ($good['level']>0)
		{
			$row[$key]['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['level']);
		}
		$row[$key]['intelligent'] = '';		
		if ($good['intelligent']>0)
		{
			$row[$key]['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['intelligent']);
		}
		$row[$key]['property'] = '';
		if ($good['property']>0)
		{
			$row[$key]['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['property']);
		}				
		//校正图片地址
//		$row[$key]['goods_img'] = OSS_HOST.get_image_path($id,$good['goods_img'],false,'goods');
		if($good['goods_img']){
			$row[$key]['goods_img'] = OSS_HOST.$good['goods_img'];
			
		}		
		
//		$row[$key]['good_count'] = 10;	
		$row[$key]['price'] = (int)$good['price'];
		
	}
			echo json_encode($row); 
}
/*获取排行榜最热列表鞋柜列表*/
else if($_REQUEST['act']=='get_hot_goods'){
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$pageNo=isset($_GET['pageNo']) ? $_GET['pageNo'] : 0 ;
	$pageSize=isset($_GET['pageSize']) ? $_GET['pageSize'] : 10 ;
	$pageNo=$pageNo-1;
	
	 $sql = "SELECT  g.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
//          "left join ".$GLOBALS['ecs']->table('goods_mark')." as gw on gw.goods_id=g.goods_id ".
            " where  ( g.is_hot = 1 OR  g.is_new = 1 ) ORDER BY g.goods_id desc ".
            "limit ".$pageNo*$pageSize." , ".$pageSize;
    $row = $GLOBALS['db']->getAll($sql);
	
	foreach ($row as $key => $good)
	{
		$row[$key]['level'] = '';		
		if ($good['level']>0)
		{
			$row[$key]['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['level']);
		}
		$row[$key]['intelligent'] = '';		
		if ($good['intelligent']>0)
		{
			$row[$key]['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['intelligent']);
		}
		$row[$key]['property'] = '';
		if ($good['property']>0)
		{
			$row[$key]['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['property']);
		}				
		//校正图片地址
//		$row[$key]['goods_img'] = OSS_HOST.get_image_path($id,$good['goods_img'],false,'goods');
		if($good['goods_img']){
			$row[$key]['goods_img'] = OSS_HOST.$good['goods_img'];
			
		}		
		
//		$row[$key]['good_count'] = 10;	
		$row[$key]['price'] = (int)$good['price'];
		
	}
			echo json_encode($row); 	
//  return array('data'=>$row, 'count'=>count($row));
//	$uu['data']	=$row;
//	$uu['count']=count($row);
//	$sql=" select g.goods_name,g.goods_id,g.level,g.property,g.intelligent,g.goods_img,shop_price,market_unit from ".$GLOBALS['ecs']->table('goods')." as g ".
//	" where  g.is_hot = 1 ".
//	" ORDER BY gh.create_time desc ".
//	"limit ".$pageNo*$pageSize." , ".$pageSize;
//	$uu=$GLOBALS['db']->getAll($sql);
//	foreach ($uu as $key => $picture)
//	{
//		if($picture['goods_img']){
//			$uu[$key]['goods_img'] = OSS_HOST.$picture['goods_img'];
//			
//		}
//		
//	}
}

/**
 * 获得商品关联的装备
 *
 * @access  public
 * @param   integer $id
 * @return  array
 */
function related_goods($goods_info ,$limit='')
{
    $sql =  'SELECT g.goods_id, g.goods_name, g.shop_price as price, g.shop_price*m.to_cny as rmb_price, g.goods_img, g.level, g.intelligent, g.property ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('money_unit') . ' AS m ON g.market_unit = m.unit_id ' .                    
            "WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.goods_id != '$goods_info[goods_id]' ";

    if ($goods_info['brand_id']>0)
    {
		$sql .= "AND g.brand_id = '$goods_info[brand_id]' ";
    }           
     
    if ($goods_info['series_id']>0)
    {
		$sql .= "AND( ";
		($goods_info['series_id']>0) ? $sql .= " g.series_id = '$goods_info[series_id]' " : " 1 ";		
	    if ($goods_info['level']>0 || $goods_info['property']>0 || $goods_info['intelligent']>0)
	    {
			$sql .= "OR( ";
			($goods_info['level']>0) ? $sql .= " g.level = '$goods_info[level]' " : " 1 ";
			($goods_info['property']>0) ? $sql .= " AND g.property = '$goods_info[property]' " : " AND 1 ";
			($goods_info['intelligent']>0) ? $sql .= " AND g.intelligent = '$goods_info[intelligent]' " : " AND 1 ";		
			$sql .= " ) ";		
	    }			
		$sql .= " ) ";		
    }
    
//  var_dump($sql);
    if ($limit)
    {    	
		$sql .= " limit " .$limit;
    }
	$res = $GLOBALS['db']->query($sql);

    $idx = 0;
    $goods = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		/* 校正商品标签 */
		$row['level']>0 ? $row['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$row['level']) : $row['level'] = "";
		$row['intelligent']>0 ? $row['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$row['intelligent']) : $row['intelligent'] ="";
		$row['property']>0 ? $row['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$row['property']) : $row['property'] ="";						
		if($row['goods_img']){
			$row['goods_img'] = OSS_HOST.$row['goods_img'];
		}		
		/* 校正商品价格 */
		if ($row['rmb_price']>0)
		{
			$row['price'] = $row['rmb_price'];
			unset($row['rmb_price']);
		}
		$row['price'] = (int)$row['price'];		
		
        $goods[$idx] = $row;
        $idx++;
    }

    return $goods;
}

/**
 * 获得商品关联的文章
 *
 * @access  public
 * @param   integer $id
 * @param   integer $type	1评测；2资讯
 * @return  array
 */
function goods_related_article($id, $type, $limit='')
{	
    /* 相关文章ID */
    $sql = "SELECT article_id FROM " . $GLOBALS['ecs']->table('goods_article') . " WHERE goods_id = ".$id;
    $row = $GLOBALS['db']->getAll($sql);	
	$article_id_arr = array_column($row, 'article_id');
	    
	/* 获取文章内容 */
	$sql = " select a.title,a.description,a.click_count,a.article_id,a.file_url as img_url,cr.copyright_type,ac.cat_name,ac.show_type from " . $GLOBALS['ecs']->table('article') . ' AS a LEFT JOIN '
				 . $GLOBALS['ecs']->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id '
				 .' LEFT JOIN '. $GLOBALS['ecs']->table('copyright') . ' AS cr ON a.copyright_id = cr.copyright_id ' ; 
	$sql .= " WHERE a.is_open = 1 AND a.article_id ".db_create_in(array_unique($article_id_arr));
	/* 获取文章总数 */
	$count_sql = " select count(*) from " . $GLOBALS['ecs']->table('article') . ' AS a LEFT JOIN '
					 . $GLOBALS['ecs']->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id '; 
	$count_sql .= " WHERE a.is_open = 1 AND a.article_id ".db_create_in(array_unique($article_id_arr));
	if ($type == 1)
	{//评测
		$sql .= " AND (ac.show_type = 1 OR ac.show_type = 2 OR ac.show_type = 3) ";			
		$count_sql .= " AND (ac.show_type = 1 OR ac.show_type = 2 OR ac.show_type = 3) ";					
	}
	else if ($type == 2)
	{//资讯
		$sql .= " AND (ac.show_type = 4 OR ac.show_type = 5) ";					
		$count_sql .= " AND (ac.show_type = 4 OR ac.show_type = 5) ";							
	}	
	$sql .= " ORDER BY a.add_time desc ";
	
	if ($limit){
		$sql .= " limit " .$limit;							
	}
    $row = $GLOBALS['db']->getAll($sql);		
	
//	var_dump($row);		
	foreach ($row as $key => $article)
	{
		//文章头图
		$article['img_url'] = OSS_HOST.get_image_path($article['article_id'],$article['img_url'],false,'artile');
//		$article_array[$key]['ImageUrl'] = getSignedUrlForGettingObject($ossClient, $bucket, OSS_HOST,get_image_path($article_data['article_id'],$article_data['file_url'],false,'artile'));
		$article['copyright_type'] == 1 ? $article['is_original'] = 1 : $article['is_original'] = 0;
		$row[$key] = $article;		
	}
			
    return array('data'=>$row, 'count'=>count($row), 'totle'=>$GLOBALS['db']->getOne($count_sql));
}

function get_technology_list ($goods_info){
//	   $sql = "SELECT g.technology_vamp, g.technology_big_bottom, g.technology_middle_bottom, g.series_id,".
//          "g.material_vamp, g.material_big_bottom,g.material_middle_bottom,a.series_name " .
//      'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
//      'LEFT JOIN ' . $GLOBALS['ecs']->table('series') . ' AS a ON a.series_id = g.series_id  ' .
////      'or a.technology_id = g.technology_big_bottom or a.technology_id = g.technology_middle_bottom '.
////      'or a.technology_id = g.material_big_bottom or a.technology_id = g.material_middle_bottom or a.technology_id = g.material_vamp '.
////      'LEFT JOIN ' . $GLOBALS['ecs']->table('series') . ' AS s ON a.technology_id = g.technology_vamp or ' .
//      "WHERE g.goods_id = '$goods_id' " ;
//  $res = $GLOBALS['db']->getRow($sql);
	$material_big_bottom =explode('|',$goods_info['material_big_bottom']);
	$material_middle_bottom =explode('|',$goods_info['material_middle_bottom']);
	$material_vamp =explode('|',$goods_info['material_vamp']);
	$technology_big_bottom =explode('|',$goods_info['technology_big_bottom']);
	$technology_middle_bottom =explode('|',$goods_info['technology_middle_bottom']);
	$technology_vamp_id =explode('|',$goods_info['technology_vamp']);
	$technology_big='';
	foreach($technology_big_bottom as $var){
		if($var!=null){
			$technology_big .=" or technology_id = ".$var;
		}
		
	}
	$technology_middle='';
//	var_dump($material_middle_bottom);
	foreach($technology_middle_bottom as $var){
	if($var!=null){	
		$technology_middle .=" or technology_id = ".$var;
	}
	}
	$technology_vamp='';
	foreach($technology_vamp_id as $var){
	if($var!=null){	
		$technology_vamp .=" or technology_id = ".$var;
	}
		
	}
	$big='';
	foreach($material_big_bottom as $var){
		if($var!=null){
			$big .=" or technology_id = ".$var;
		}
		
	}
	$middle='';
//	var_dump($material_middle_bottom);
	foreach($material_middle_bottom as $var){
	if($var!=null){	
		$middle .=" or technology_id = ".$var;
	}
	}
	$vamp='';
	foreach($material_vamp as $var){
		if($var!=null){	
			$vamp .=" or technology_id = ".$var;
		}
		
	}

	$sql = "SELECT technology_name,technology_cat,cat_type FROM " . $GLOBALS['ecs']->table('technology') .
	" where technology_id =0 ".$technology_big.$technology_middle.$technology_vamp.$big.$middle.$vamp.
	" order by cat_type,technology_cat";
    $technology = $GLOBALS['db']->getAll($sql);
	$cat ="";
    foreach($technology as $key=>$var){
		
		if($var['cat_type']==0){
			if($var['technology_cat']==1)
			{
				if(isset($big_key)){
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="";
				}
				else{
					$big_key=$key;
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="大底材料";
				}
				
			}
			else if($var['technology_cat']==2)
			{
				if(isset($middle_key)){
					
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="";
				}else{
					$middle_key=$key;
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="中底材料";
				}
				
			}
			else if($var['technology_cat']==3)
			{
				if(isset($vamp_key)){
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="";
				}else{
					$vamp_key=$key;
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="鞋面材料";
				}
				
			}
		}
		else if($var['cat_type']==1)
		{
			if($var['technology_cat']==1)
			{
				if(isset($technology_big_key)){
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="";
				}else{
					$technology_big_key=$key;
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="大底科技";
				}

			}
			else if($var['technology_cat']==2)
			{
				if(isset($technology_middle_key)){
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="";
				}else{
					$technology_middle_key=$key;
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="中底科技";
				}
			}
			else if($var['technology_cat']==3)
			{
				if(isset($technology_vamp_key)){
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="";
				}else{
					$technology_vamp_key=$key;
					$cat[$key]['technology_name']=$var['technology_name'];
					$cat[$key]['cat_name']="鞋面科技";
				}
				
			}
			
		}

	}
	$cat[count($cat)]['technology_name']=$goods_info['series_name'];
	$cat[count($cat)-1]['cat_name']="所属系列";
	return $cat;
}


?>